<template>
  <div class="register-container">
    <el-card class="register-card" shadow="hover">
      <h2 class="register-title">注册账号 Nebula Admin</h2>

      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <!-- 用户名 -->
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username" placeholder="请输入注册账号" />
        </el-form-item>

        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" show-password placeholder="请输入密码" />
        </el-form-item>

        <!-- 确认密码 -->
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="form.confirmPassword" type="password" show-password placeholder="请再次输入密码" />
        </el-form-item>

        <!-- 注册按钮 -->
        <el-form-item>
          <el-button type="primary" @click="handleRegister" :loading="loading">
            注册
          </el-button>
        </el-form-item>

        <!-- 登录链接 -->
        <div class="login-link">
          已有账号？
          <span class="link" @click="goLogin">去登录</span>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { register } from '@/api/user'

// 表单数据
const form = reactive({
  username: '',
  password: '',
  confirmPassword: ''
})

// 验证规则
const rules = {
  username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ]
}

// 自定义确认密码校验方法
function validateConfirmPassword(rule, value, callback) {
  if (value !== form.password) {
    callback(new Error('两次密码不一致'))
  } else {
    callback()
  }
}

// 控制加载状态
const loading = ref(false)

// 引用表单
const formRef = ref(null)

// 路由跳转
const router = useRouter()

// 注册方法
const handleRegister = () => {
  formRef.value.validate(async (valid) => {
    if (!valid) return

    loading.value = true
    try {
      await register({
        username: form.username,
        password: form.password
      })
      ElMessage.success('注册成功！请登录')
      router.push('/login')
    } catch (error) {
      ElMessage.error(error.message || '注册失败')
    } finally {
      loading.value = false
    }
  })
}

// 跳转登录页
const goLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.register-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: url('@/assets/bg.png') no-repeat center center;
  background-size: cover;
}

.register-card {
  width: 420px;
  padding: 20px;
}

.register-title {
  text-align: center;
  margin-bottom: 20px;
}

.login-link {
  text-align: right;
  margin-top: 10px;
  font-size: 14px;
}

.login-link .link {
  color: #409EFF;
  cursor: pointer;
}

.login-link .link:hover {
  text-decoration: underline;
}
</style>
